<?php

namespace Tui\Controller;
use Common\Controller\AdminbaseController;
class AgentController extends AdminbaseController {
    function _initialize() {
		parent::_initialize();	
        $this->row=20;
	}
    
    public function index(){
        $this->show("ok");
    }
    
    public function check(){
        
        
        $this->show("check");
    }
    
    
    public function checkinfo(){
    
        $model=M('users');
        
        $count=$model->where("user_type=6")->count();           
        $page=$this->page($count,$this->row);                
        $users=$model
                ->field('u.*,am.*')
                ->alias('u')
                ->join("LEFT JOIN ".C('DB_PREFIX')."agent_man am ON u.id=am.agent_id")
                ->where("u.user_type=6")
                ->order("id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();                                                   
        
        $this->assign("users",$users);
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->display();
    }
    
    
    
    private function get_agent_ext_info(){
        return M('agent_ext')->where(array("agent_id"=>$_SESSION['agent_id']))->find();
    }
    
    
    
    
    
    
    
    public function params(){
        
        $this->assign("base",  get_agent_withdraw_base());
        $this->display();
    }
    public function params_post(){
        $base=I('agent_widthdraw_base/d');
//        echo $base;
        if($base>=0){
            M('options')->where(array("option_id"=>1))->setField("option_value","$base");
//            set_option("agent_widthdraw_base", $base);
            $this->success("提现阈值设置成功");
        }else{
            $this->error("提现阈值必须大于或者等于0");
        }
        
    }
    
    public function export(){
        $type=I('path.2');
        
        $items=array();
        $fname='数据导出';
        
        if($type=="cr"){
            $fname="充值记录";
            $items=$this->export_cr();
        }else if($type=="vc"){
            $fname="代金券消费记录";
            $items=$this->export_vc();
        }else if($type=="mvc"){
            $fname="玩家充值代金券记录";
            $items=$this->export_mvc();
        }else if($type=="ex"){
            $fname="余额兑换代金券记录";
            $items=$this->export_ex();
        }
        $this->export_do($items,$fname);
    }
    
    public function export_vc(){
        $items=M('gm_pay')
                ->field("ap.id as ID,ap.amount as 消费金额, u.user_nicename as 代理用户名,u.mobile as 代理手机号,u.user_login as 代理名,m.username as 玩家名,g.name as 游戏名")
                ->alias('ap')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ap.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ap.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ap.mem_id")
                ->order("ap.id desc")
                ->limit(300)
                ->select();
        return $items; 
    }
    public function export_mvc(){
        $items=M('gm_charge')
                ->field("ac.id as ID,u.user_nicename as 代理用户名,u.mobile as 代理手机号,u.user_login,m.username as 玩家名,g.name as 游戏名,"
                        . "agr.agent_rate as 平台折扣,(ac.discount-agr.agent_rate)*ac.money as 利润")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ac.mem_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game ag ON ((ag.agent_id=ac.admin_id) AND (ag.app_id = ac.app_id))")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON ((agr.agent_id=ac.admin_id) AND (agr.app_id = ac.app_id))")
                ->order("ac.id desc")
                ->limit(300)
                ->select();
        return $items; 
    }
    public function export_ex(){
        $items=M('gm_agentcharge')
                ->field("ac.id as ID,u.user_nicename as 用户名,u.mobile as 用户手机号,u.user_login,g.name as 游戏名")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->order("ac.id desc")
                ->limit(300)
                ->select();
        return $items; 
    }
    public function export_cr(){
        $items=M('gm_charge')
                ->field("ac.id as ID,u.user_nicename as 代理用户名,u.mobile as 代理手机号,m.username as 玩家用户名,g.name as 游戏名,g.game_rate as 成本,(ac.discount-g.game_rate)*ac.money as 利润")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ac.mem_id")
                ->order("ac.id desc")
                ->limit(300)
                ->select();
        return $items;    
    }
    
    
    
    public function export_do($items,$file_name="后台数据导出"){
    
        
Vendor("PHPExcel.PHPExcel");

// Create new PHPExcel object
$objPHPExcel = new \PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("严旭")
							 ->setLastModifiedBy("严旭")
							 ->setTitle("后台数据导出")
							 ->setSubject("后台数据导出")
							 ->setDescription("后台数据导出")
							 ->setKeywords("后台数据导出")
							 ->setCategory("后台数据导出");

if(count($items)>=1){
    $c=$items[0];
    $c_i=0;
    foreach($c as $column=>$v){
        $left_txt=chr(65+$c_i)."1";
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue("$left_txt", $column);
        $c_i++;
    }
}
$row=1;
foreach($items as $key=>$item){
    $i=0;
    foreach($item as $column=>$value){
        $index=$row+1;
        $left_txt=chr(65+$i)."$index";
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue("$left_txt", $value);
        $i++;
    }
    $row++;
}

// Add some data
//$objPHPExcel->setActiveSheetIndex(0)
//            ->setCellValue('A1', 'Hello')
//            ->setCellValue('B2', 'world!')
//            ->setCellValue('C1', 'Hello')
//            ->setCellValue('D2', 'world!');

// Miscellaneous glyphs, UTF-8
//$objPHPExcel->setActiveSheetIndex(0)
//            ->setCellValue('A4', 'Miscellaneous glyphs')
//            ->setCellValue('A5', 'test');

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Data');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$file_name.'.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
    }
    
    public function chargeRecord(){                
        $where=array();
        
        if(isset($_GET['agent_id'])&&($_GET['agent_id'])){
            $where=array("ac.admin_id"=>$_GET['agent_id']);
        }
        
        if(isset($_GET['mem_id'])&&($_GET['mem_id'])){
            $where=array("ac.mem_id"=>$_GET['mem_id']);
        }
        
        $hs_where_obj=new \HuoShu\Where();
        $hs_where_obj->time($where,"ac.create_time");
        
        $count=M('gm_charge')->alias('ac')->where($where)->count();
        $page=$this->page($count,$this->row);

        $items=M('gm_charge')
                ->field("ac.*,u.user_nicename,u.mobile,u.user_login,m.username as memname,g.name as gamename,g.game_rate as cost,(ac.discount-g.game_rate)*ac.money as profit")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ac.mem_id")
                ->where($where)
                ->order("ac.id desc")
                ->limit($page->firstRow , $page->listRows)
                ->select();
        
        $sumitems=M('gm_charge')
                ->field("sum(money) as sum_money,sum(gm_cnt) as sum_cnt,sum((ac.discount-g.game_rate)*ac.money) as sum_profit ")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->where($where)
                ->select();
        
      
        
//        foreach($items as $key=>$value){
//            $items["$key"]['profit']=($value['discount']-$value['cost'])*$value['money'];
//        }
        
        $agents=get_all_agents();
        $this->assign("agents",$agents);
        $this->assign("formget",$_GET);
        
        $hs_filter_obj=new \HuoShu\UI\Filter();
        $mem_select_txt=$hs_filter_obj->member_select();
        $this->assign("mem_select_txt",$mem_select_txt);
        $agent_select_txt=$hs_filter_obj->agent_select2();
        $this->assign("agent_select_txt",$agent_select_txt);
        $this->assign("time_choose_txt",$hs_filter_obj->time_choose());
        
        $this->assign("sumitems", $sumitems);
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    }
    public function voucherConsumption(){

        $where=array();

        if(isset($_GET['memname'])&&$_GET['memname']!=''){
            $mem_id=  get_memid_by_name($_GET['memname']);
            $where["ap.mem_id"]=$mem_id;  
        }
        
        if(isset($_GET['gamename'])&&$_GET['gamename']!=''){
            $name=$_GET['gamename'];
            $app_id=M('game')->where(array("name"=>$name))->getField("id");
            $where["ap.app_id"]=$app_id;    
        }
        
        
        if(isset($_GET['agent'])&&$_GET['agent']!=''){
            $where["ap.agent_id"]=$_GET['agent'];  
        }
        
        $model=M('gm_pay');
        $count=$model->alias('ap')->where($where)->count();
        $page=$this->page($count,$this->row);

        $items=$model
                ->field("ap.*,u.user_nicename,u.mobile,u.user_login as agentname,m.username as memname,g.name as gamename")
                ->alias('ap')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ap.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ap.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ap.mem_id")
                ->where($where)
                ->order("ap.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $sumitems=$model
                ->field("sum(amount) as sum_amount,sum(gm_cnt) as sum_cnt")
                ->alias('ap')
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        $agents=get_all_agents();
        $this->assign("agents",$agents);
        $this->assign("formget",$_GET);
        
        $this->assign("memname", $memname);
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    }
    
    public function exchange(){
        
        $where=array();
        
        if(isset($_GET['agent'])&&($_GET['agent']!='')){
            $where=array("ac.agent_id"=>$_GET['agent']);
        }
        
        $count=M('gm_agentcharge')->alias('ac')->where($where)->count();
        $page=$this->page($count,$this->row);

        $items=M('gm_agentcharge')
                ->field("ac.*,u.user_nicename,u.mobile,u.user_login,g.name as gamename")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->where($where)
                ->order("ac.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $sumitems=M('gm_agentcharge')
                ->field("sum(money) as sum_money,sum(gm_cnt) as sum_cnt")
                ->alias('ac')
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        
        $agents=get_all_agents();
        $this->assign("agents",$agents);
        $this->assign("formget",$_GET);
        
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    }
    
    public function memberVoucherCharge(){
        $where=array();

        if(isset($_GET['agent'])&&$_GET['agent']!=''){
            $where["ac.admin_id"]=array('eq',$_GET['agent']);
        }
        
        if(isset($_GET['memname'])&&$_GET['memname']!=''){
    
            $mem_id=get_memid_by_name($_GET['memname']);
            $where["ac.mem_id"]= array('eq',$mem_id) ;
        }
        
        if(isset($_GET['orderid'])&&$_GET['orderid']!=''){
            $where["ac.order_id"]=array('eq',$_GET['orderid']);
        }
        
        if(isset($_GET['start_time'])&&$_GET['start_time']!=''){
            $where["ac.create_time"]=array('gt',strtotime($_GET['start_time']));
        }
        
        if(isset($_GET['end_time'])&&$_GET['end_time']!=''){
            $where["ac.create_time"]=array('lt',strtotime($_GET['end_time']));
        }
        
        
        $count=M('gm_charge')->alias('ac')->where($where)->count();
        $page=$this->page($count,$this->row);

        $items=M('gm_charge')
                ->field("ac.*,u.user_nicename,u.mobile,u.user_login,m.username as memname,g.name as gamename,"
                        . "agr.agent_rate as platrate,(ac.discount-agr.agent_rate)*ac.money as profit")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ac.admin_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ac.app_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."members m ON m.id=ac.mem_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game ag ON ((ag.agent_id=ac.admin_id) AND (ag.app_id = ac.app_id))")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON ((agr.agent_id=ac.admin_id) AND (agr.app_id = ac.app_id))")
                ->where($where)
                ->order("ac.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $sumitems=M('gm_charge')
                ->field("sum(money) as sum_money,sum(gm_cnt) as sum_cnt,sum((ac.discount-agr.agent_rate)*ac.money) as sum_profit")
                ->alias('ac')
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game ag ON ((ag.agent_id=ac.admin_id) AND (ag.app_id = ac.app_id))")
                ->join("LEFT JOIN ".C("DB_PREFIX")."agent_game_rate agr ON ((agr.agent_id=ac.admin_id) AND (agr.app_id = ac.app_id))")
                ->where($where)
                ->select();
        $this->assign("sumitems", $sumitems);
        
        $agents=get_all_agents();
        $this->assign("agents",$agents);       
        $this->assign("formget", $_GET);
        
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    } 
    
    public function get_all_apply_agents(){
        $items= M('agent_game')
                ->field("DISTINCT agent_id,u.user_login")
                ->alias('ag')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ag.agent_id")
                ->order("u.id desc")
                ->select();
        return $items;
    }
    
    public function get_all_apply_games(){
        $items= M('agent_game')
                ->field("DISTINCT app_id,g.name as gamename")
                ->alias('ag')
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ag.app_id")
                ->order("g.id desc")
                ->select();
        return $items;
    }
    
    public function setGameAllAgentRate(){
        $where=array();
        
        if(isset($_GET['gamename'])&&($_GET['gamename']!='')){
            $gamename=$_GET['gamename'];
            $where['g.name']= array("like","%$gamename%");
        }
       $model=M('game');
//        
        $count=$model->alias('g')->where($where)->count();
        $page=$this->page($count,20);
               
        
        $items=$model
                ->alias('g')
                ->where($where)
                ->order("g.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();

        $this->assign("formget",$_GET);
        
        
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        
        $this->assign("items",$items);
        $this->display();
    }
    
    public function setGameAllAgentRate_post(){
        $app_id=I('id');
        $new_rate=I('new_rate');
        
        if(!( is_numeric($new_rate) && $new_rate>0 && $new_rate<1 )){
            $this->ajaxReturn(array("error"=>"1","msg"=>"折扣必须是介于0和1之间的数字"));
            exit;
        }
        
//        M('agent_game')->where(array("app_id"=>$app_id))->setField("agent_rate",$new_rate);
        
        $hs_benefit_obj=new \HuoShu\Benefit();
        $hs_benefit_obj->set_app_agent_rate($app_id, $new_rate);
//        M('game_rate')->where(array("id"=>$app_id))->setField("agent_rate",$new_rate);
        $hs_benefit_obj->set_app_agentgame_all_agentrate($app_id, $new_rate);
              
        $this->ajaxReturn(array("error"=>"0","msg"=>"设置成功"));
    }
     public function setGameAllAgentRebate_post(){
        $app_id=I('id');
        $new_rate=I('value');
        
        if(!( is_numeric($new_rate) && $new_rate>0 && $new_rate<1 )){
            $this->ajaxReturn(array("error"=>"1","msg"=>"返点必须是介于0和1之间的数字"));
            exit;
        }
        
        M('agent_game')->where(array("app_id"=>$app_id))->setField("rebate",$new_rate);
        M('game')->where(array("id"=>$app_id))->setField("rebate",$new_rate);
              
        $this->ajaxReturn(array("error"=>"0","msg"=>"设置成功"));
    }
    
    
    public function Discount(){
        $where=array();
        
        if(isset($_GET['appid'])&&($_GET['appid']!=-1)){
            $appid=$_GET['appid'];
            $where['ag.app_id']= $appid;
        }
        
        if(isset($_GET['agent'])&&($_GET['agent']!='')){
            $agentid=$_GET['agent'];
            $where['ag.agent_id']= $agentid;
        }
        
        
        $count=M('agent_game')->alias('ag')->where($where)->count();
        $page=$this->page($count,20);
               
        
        $items=M('agent_game')
                ->field("ag.*,u.user_nicename,u.mobile,u.user_login,g.name as gamename")
                ->alias('ag')
                ->join("LEFT JOIN ".C("DB_PREFIX")."users u ON u.id=ag.agent_id")
                ->join("LEFT JOIN ".C("DB_PREFIX")."game g ON g.id=ag.app_id")
                ->where($where)
                ->order("ag.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        
        $games=$this->get_all_apply_games();
        
        $this->assign("games",$games);
        
//        $agents=$this->get_all_apply_agents();
//        $this->assign("agents",$agents);
//        $this->assign("query_field",$query_field);
        
        $agents=get_agent_game_all_agents();
        $this->assign("agents",$agents);
        $this->assign("formget",$_GET);
        
        
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    }     
    
    
    public function notpass(){
        M('users')->where(array("id"=>I('id'),"user_type"=>"7"))->setField("user_status","3");
        $this->success("修改成功");
    }
    
    public function pass(){       
        
        M('users')->where(array("id"=>I('id'),"user_type"=>"7"))->setField("user_status","2");
        
        Vendor("LanzSMS");
        $obj=new \LanzSMS();
        $userphone= M('users')->where(array("id"=>I('id'),"user_type"=>"7"))->getField("mobile");
        $obj->sendMsg_simple($userphone, "恭喜，您已成功通过资料审核。");
        
        $this->success("修改成功");
    }
    
//    public function testSMS(){
//        Vendor("LanzSMS");
//        $obj=new \LanzSMS();
////        $userphone= M('users')->where(array("id"=>I('id'),"user_type"=>"7"))->getField("mobile");
//        $userphone='15914517759';
//        echo $userphone;
//        $obj->sendMsg_simple($userphone, "恭喜，您已成功通过资料审核。【芒果玩游戏】");
//        echo ' hello';
//    }
    
    
    
    public function change_agent_rate(){
        $new_rate=I('new_rate');
        $ag_id=I('id');
        
        if(!( is_numeric($new_rate) && $new_rate>0 && $new_rate<1 )){
            $this->ajaxReturn(array("error"=>"1","msg"=>"折扣必须是介于0和1之间的数字"));
            exit;
        }
//        $base=$this->get_rate_base($app_id);
//        if($new_rate < $base){
//            $this->ajaxReturn(array("error"=>"1","msg"=>"折扣不能低于基线".$base));
//            exit;
//        }
        $hs_benefit_obj=new \HuoShu\Benefit();
        $hs_benefit_obj->set_app_agentgame_agentrate($ag_id, $new_rate);
//        M('agent_game_rate')->where(array("id"=>$app_id))->setField("agent_rate",$new_rate);
        $this->ajaxReturn(array("error"=>"0","msg"=>"修改成功"));
        
        
    }
    
    
    public function change_agent_rebate(){
        $new_rate=I('new_rate');
        $app_id=I('id');
        
        if(!( is_numeric($new_rate) && $new_rate>0 && $new_rate<1 )){
            $this->ajaxReturn(array("error"=>"1","msg"=>"折扣必须是介于0和1之间的数字"));
            exit;
        }
        
        M('agent_game')->where(array("id"=>$app_id))->setField("rebate",$new_rate);
        $this->ajaxReturn(array("error"=>"0","msg"=>"修改成功"));
    }
    
    public function resetPayPwd_post(){
        $id=I('id');
        $obj=new \HuoShu\Account();
        $obj->resetUserPayPwd($id);
        $this->ajaxReturn(array("error"=>"0","msg"=>"重置支付密码成功"));
    }
    
    public function resetPayPwd(){
        $where=array();
        $where['_string']= "user_type=6 OR user_type=7";
        
//        if(isset($_GET['appid'])&&($_GET['appid']!=-1)){
//            $appid=$_GET['appid'];
//            $where['ag.app_id']= $appid;
//        }
//        
        if(isset($_GET['agent'])&&($_GET['agent']!='')){
            $agent=$_GET['agent'];
            $where['u.user_login']= array("like","%$agent%");
        }
//        
//        
        $count=M('users')->alias('u')->where($where)->count();
        $page=$this->page($count,20);
               
        
        $items=M('users')
                ->alias('u')
                ->where($where)
                ->order("u.id desc")
                ->limit($page->firstRow . ',' . $page->listRows)
                ->select();
        $this->set_user_type($items);
        $this->assign("formget",$_GET);
        
        
        $this->assign("Page", $page->show('Admin'));
	    $this->assign("current_page", $page->GetCurrentPage());
        $this->assign("items",$items);
        $this->display();
    }
    
    public function set_user_type(&$items){
        foreach($items as $key=>$member){
            $type=$items[$key]['user_type'];
            if($type==6){
                $items[$key]['user_type']='代理';
            }else if($type==7){
                $items[$key]['user_type']='下级代理';
            }
        }
    }
    
    public function memGmRechargeRecord(){
        
        $hs_data_ptbmem_obj=new \HuoShu\Data\PtbMem();
       
        $table_header=$hs_data_ptbmem_obj->get_table_header_admin();
        $this->assign("table_header",$table_header);       
        
        $count=$hs_data_ptbmem_obj->get_admin_count();
        $page=$this->page($count,10);                
        $table_content=$hs_data_ptbmem_obj->get_admin_table($page->firstRow,$page->listRows);
        $this->assign("table_content",$table_content);
        
        $this->assign("Page", $page->show('Admin'));
        
        $sum_txt=$hs_data_ptbmem_obj->get_sum_admin_txt(array());
        $this->assign("table_sum",$sum_txt);

        $this->display();
    }
    
    public function memChargeforgame(){
        $this->_game();
        $this->_payway();
        $this->_paystatus();
        $this->_orderList();
        $this->display();
    }
    
    function _payway($type=NULL, $option=true){
        $cates=array(
            ""=>"全部"
        );
        $payways = M('payway')->getField("payname,realname", true);
        if($option){
            $payways=$cates + $payways;
        }
    
        $this->assign("payways",$payways);
    }
    function _paystatus(){
        $cates=array(
            "2"=>"支付成功",
            "1"=>"待支付",
            "3"=>"支付失败",
        );
        $this->assign("paystatuss",$cates);
    }
    
    function _orderList() {
        $where_ands = array("p.agent_id".$this->agentwhere);
        array_push($where_ands, "p.payway != '0'");
        if($_POST['paystatus'] == ''){
            $string = "p.status = 2 ";
            array_push($where_ands, $string);
        }
    
//         $fields = array(
//             'start_time' => array(
//                 "field" => "p.create_time",
//                 "operator" => ">"
//             ),
//             'end_time' => array(
//                 "field" => "p.create_time",
//                 "operator" => "<"
//             ),
//             'orderid' => array(
//                 "field" => "p.order_id",
//                 "operator" => "="
//             ),
//             'gid' => array(
//                 "field" => "p.app_id",
//                 "operator" => "="
//             ),
//             'username' => array(
//                 "field" => "m.username",
//                 "operator" => "="
//             ),
//             'payway' => array(
//                 "field" => "p.payway",
//                 "operator" => "="
//             ),
//             'paystatus' => array(
//                 "field" => "p.status",
//                 "operator" => "="
//             ),
//             'benefittype' => array(
//                 "field" => "u.user_nicename",
//                 "operator" => "="
//             )
//        );
//         if (IS_POST) {
//             foreach ($fields as $param => $val) {
//                 if (isset($_POST[$param]) && !empty($_POST[$param])) {
//                     $operator = $val['operator'];
//                     $field = $val['field'];
//                     $get = trim($_POST[$param]);
//                     $_GET[$param] = $get;
    
//                     if ('start_time' == $param) {
//                         $get = strtotime($get);
//                     } else if ('end_time' == $param) {
//                         $get .= " 23:59:59";
//                         $get = strtotime($get);
//                     } else if ('cpstatus' == $param){
//                         $get = intval($get);
//                         if (2 == $get){
//                             array_push($where_ands, "$field $operator '$get'");
//                             array_push($where_ands, "p.status = 2");
//                         }else if (1 == $get){
//                             array_push($where_ands, "$field $operator '$get'");
//                             array_push($where_ands, "p.status = 1");
//                         }else{
//                             array_push($where_ands, "p.cpstatus != 2");
//                             array_push($where_ands, "p.status = 2");
//                         }
//                         continue;
//                     }
    
//                     if ($operator == "like") {
//                         $get = "%$get%";
//                     }
//                     array_push($where_ands, "$field $operator '$get'");
//                 }
//             }
//         }else{
//             foreach ($fields as $param => $val) {
//                 if (isset($_GET[$param]) && !empty($_GET[$param])) {
//                     $operator = $val['operator'];
//                     $field = $val['field'];
//                     $get = trim($_GET[$param]);
    
//                     if ('start_time' == $param) {
//                         $get = strtotime($get);
//                     } else if ('end_time' == $param) {
//                         $get .= " 23:59:59";
//                         $get = strtotime($get);
//                     }else if ('cpstatus' == $param){
//                         $get = intval($get);
//                         if (2 == $get){
//                             array_push($where_ands, "$field $operator '$get'");
//                             array_push($where_ands, "p.status = 2");
//                         }else if (1 == $get){
//                             array_push($where_ands, "$field $operator '$get'");
//                             array_push($where_ands, "p.status = 1");
//                         }else{
//                             array_push($where_ands, "p.cpstatus != 2");
//                             array_push($where_ands, "p.status = 2");
//                         }
//                         continue;
//                     }
    
//                     if ($operator == "like") {
//                         $get = "%$get%";
//                     }
//                     array_push($where_ands, "$field $operator '$get'");
//                 }
//             }
//         }
    
//         if ($_POST['firstagentname']) {
//             $_GET['firstagentname'] = $_POST['firstagentname'];
//         }
    
//         $firstagentname = I('get.firstagentname/s','');
//         if(!empty($firstagentname)){
//             if($firstagentname == "default"){
//                 $string = " p.agent_id = 0 ";
//             }else{
//                 $string = "u.user_login = '".$firstagentname."'";
//             }
//             array_push($where_ands, $string);
//         }
    
//         $where = join(" and ", $where_ands);
    
        $count = M("pay")
        ->alias("p")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->where($where)
        ->count();
    
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : $this->row;
        $page = $this->page($count, $rows);
    
        $field = "p.order_id, p.amount,p.real_amount,p.rebate_cnt, m.username,p.agent_id, p.payway, u.user_login agentname, u.user_nicename agentnickname, g.name gamename,p.status,p.cpstatus, p.create_time, p.app_id";
    
        $items = M("pay")
        ->alias("p")
        ->field($field)
        ->where($where)
        ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->order("p.id DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
    
        $sums = M("pay")
        ->alias("p")
        ->where($where)
        ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->sum('amount');
    
        $realsum = M("pay")
        ->alias("p")
        ->where($where)
        ->join("left join " . C('DB_PREFIX') . "game g ON p.app_id = g.id")
        ->join("left join " . C('DB_PREFIX') . "users u ON p.agent_id = u.id")
        ->join("left join " . C('DB_PREFIX') . "members m ON p.mem_id = m.id")
        ->sum('real_amount');
    
        $this->assign("realsum",$realsum);
        $this->assign("sums", $sums);
        $this->assign("orders", $items);
        $this->assign("formget", $_GET);
        $this->assign("Page", $page->show('Admin'));
        $this->assign("current_page", $page->GetCurrentPage());
    }
}

